<?php
/*******************************************************************************

    FinalsClub.org is a platform for students and faculty to collaborate
    and share their knowledge with the world.

    Copyright (C) 2009  Andrew Magliozzi
 
    This file is a part of FinalsClub.org.

    FinalsClub.org is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
    To contact FinalsClub.org, send an email to info@finalsclub.org, or
    send mail to Finals Club, c/o Veritas Tutors, 1132 Massachusetts Avenue,
    Cambridge, MA 02138.

*******************************************************************************/

class Group extends BaseGroup
{
	public function __toString(){
		return $this->getName();
	}
		/* CLH - Simply returns a hyperlinked name for the object */
	public function renderAsRecipient(){
		return "<a href = '/groups/show/id/".$this->getId()."'>".$this->getName()."</a>";
	} 

	public function save(PropelPDO $con = null) {
		parent::save($con);		// we need to save the course first
		
		//activity feed
		$activity_feed = new ActivityFeed();
		$user = UserPeer::retrieveById($this->getUserId());		// note author
		$user_id = $user->getId();

		$activity_feed->setUserId($user_id);		// set who wrote the note
		$activity_feed->setType(ActivityFeedPeer::GROUP_TYPE);				// set activity feed type (note)
		$activity_feed->setTypeId($this->getId());
		$message = "<strong>created the group</strong>";	// the majority of the message gets set here
		$activity_feed->setMessage($message);
		$activity_feed->save();
		
		$this->updateLuceneIndex();  // update Lucene index
	}

	public function delete(PropelPDO $con = null)
	{
		$index = Search::getLuceneIndex(Search::GROUP);
		$term  = new Zend_Search_Lucene_Index_Term($this->getId(), 'pk');
		$query = new Zend_Search_Lucene_Search_Query_Term($term);
		$hit   = $index->find($query);
		if ($hit) {
  			foreach($hit as $h) { $index->delete($h->id); }
		}

		parent::delete($con);
	}

	public function updateLuceneIndex() {
  		$index = Search::getLuceneIndex(Search::GROUP);
 
  		// remove an existing entry
		$term  = new Zend_Search_Lucene_Index_Term($this->getId(), 'pk');
		$query = new Zend_Search_Lucene_Search_Query_Term($term);
		$hit   = $index->find($query);
  		if ($hit) {
    			foreach($hit as $h) { $index->delete($h->id); }
  		}
 
  		$doc = new Zend_Search_Lucene_Document();
 
	  	// store primary key URL to identify it in the search results
	  	$doc->addField(Zend_Search_Lucene_Field::Keyword('pk', $this->getId()));
 
	  	// index fields
	  	$doc->addField(Zend_Search_Lucene_Field::Text('name', $this->getName()));
	  	$doc->addField(Zend_Search_Lucene_Field::Text('description', $this->getDescription()));
  		// add to the index
  		$index->addDocument($doc);
  		$index->commit();
  	}
}
